﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class 注册窗口 : Form
    {
        public 注册窗口()
        {
            InitializeComponent();
        }

        private void Label1_Click(object sender, EventArgs e)
        {

        }

        private void TextUid_Leave(object sender, EventArgs e)
        {
            //鼠标离开时，验证用户名是否存在
            String uid = txtUid.Text.Trim();
            if (uid.Length == 0) return;
            // 不为空时验证
            string sql = "select count(*) from LoginTable where uid='" + uid + "'";
            Object result =SQLHelper.ExecuteScalar(sql);
            int r = Convert.ToInt32(result);
            if (r>0)
            {
                lbMsg.ForeColor = Color.Red;
                lbMsg.Text = "该用户已经注册，请重新输入用户名";
            }
            else {
                lbMsg.ForeColor = Color.Green;
                lbMsg.Text = "✔";
            }
        }

        private void BtnReg_Click(object sender, EventArgs e)
        {
            //验证
            string uid = txtUid.Text.Trim();
            string pwd1 = txtPwd1.Text;
            string pwd2 = txtPwd2.Text;

            // 空
            if (uid.Length == 0 || pwd1.Length == 0 || pwd2.Length == 0) {
                return;
            }
            //两次密码相等
            if (pwd1 != pwd2){
                return;
            }
            // 用户名是否可用
            if (lbMsg.Text != "✔") return;
            //  插入数据
            string sql = "insert into LoginTable (uid,pwd) values ('" + uid + "','" + pwd1 + "')";
            int count=SQLHelper.ExecuteNonQuery(sql);
            if (count > 0){
                MessageBox.Show("注册成功");
                // 注册窗体
                this.Dispose();
            }else {
                MessageBox.Show("Error");
            }
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
